package org.csu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.demo.common.CommonResponse;
import org.csu.demo.entity.Category;
import org.csu.demo.entity.Item;
import org.csu.demo.entity.ItemInventory;
import org.csu.demo.entity.Product;
import org.csu.demo.persistence.CategoryMapper;
import org.csu.demo.persistence.ItemInventoryMapper;
import org.csu.demo.persistence.ItemMapper;
import org.csu.demo.persistence.ProductMapper;
import org.csu.demo.service.CatalogService;
import org.csu.demo.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("catalogService")
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ItemInventoryMapper itemInventoryMapper;

    @Override
    public CommonResponse<List<Category>> getCategoryList(){
        List<Category> categoryList = categoryMapper.selectList(null);
        if (categoryList.isEmpty()){
            return CommonResponse.crateForSuccessMessage("no information about category");
        }

        return CommonResponse.crateForSuccess(categoryList);
    }

    @Override
    public CommonResponse<Category> getCategoryById(String categoryId) {
        Category category = categoryMapper.selectById(categoryId);
        if (category == null){
            return CommonResponse.crateForSuccessMessage("No category has such id");
        }

        return CommonResponse.crateForSuccess(category);
    }

    @Override
    public CommonResponse<List<Product>> getProductListByCategoryId(String productId) {
        List<Product> productList = productMapper.selectList(null);
        if (productList.isEmpty()){
            return CommonResponse.crateForSuccessMessage("no information about product");
        }

        return CommonResponse.crateForSuccess(productList);
    }

    @Override
    public CommonResponse<Product> getProductById(String productId) {
        Product product = productMapper.selectById(productId);
        if (product == null){
            return CommonResponse.crateForSuccessMessage("No product has such id");
        }

        return CommonResponse.crateForSuccess(product);
    }

    @Override
    public CommonResponse<List<ItemVO>> getItemsByProductId(String productId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("productid",productId);
        List<Item> itemList = itemMapper.selectList(queryWrapper);

        if (itemList.isEmpty()){
            return CommonResponse.crateForError("this product dose not have any item");
        }

        Product product = productMapper.selectById(productId);

        List<ItemVO> itemVOList = new ArrayList<>();
        for(Item item : itemList){
            ItemVO itemVO = itemToItemVO(item,product);
            itemVOList.add(itemVO);
        }

        return CommonResponse.crateForSuccess(itemVOList);
    }

    private ItemVO itemToItemVO(Item item, Product product){
//        设置Item表中的信息
        ItemVO itemVO = new ItemVO();
        itemVO.setItemId(item.getItemId());
        itemVO.setProductId(item.getProductId());
        itemVO.setListPrice(item.getListPrice());
        itemVO.setUnitCost(item.getUnitCost());
        itemVO.setSupplierId(item.getSupplierId());
        itemVO.setStatus(item.getStatus());
        itemVO.setAttribute1(item.getAttribute1());
        itemVO.setAttribute2(item.getAttribute2());
        itemVO.setAttribute3(item.getAttribute3());
        itemVO.setAttribute4(item.getAttribute4());
        itemVO.setAttribute5(item.getAttribute5());

//        设置Product表中的信息
        itemVO.setCategoryId(product.getCategoryId());
        itemVO.setProductName(product.getName());
        itemVO.setProductDescription(product.getDescription());
        itemVO.setProductDescriptionWords(product.getDescriptionWords());

//        设置ItemInventory表中的信息
        ItemInventory itemInventory = (ItemInventory)itemInventoryMapper.selectById(item.getItemId());
        itemVO.setQuantity(itemInventory.getQuantity());

        return itemVO;
    }

}
